<?php

declare(strict_types=1);

namespace App\Controller\m1;

use Throwable;
use App\Helper\Log;
use App\Helper\Util;
use App\Helper\Common;
use App\Helper\Verify;
use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\Manager\AuthMiddleware;
use App\Service\OfficeService;

#[Controller]
#[Middleware(AuthMiddleware::class)]
class StaticController extends AbstractController
{
    #每日统计
    #[RequestMapping(path: "everyday", methods: "post")]
    public function everyday()
    {
        $device = $this->request->input('device', '');//设备
        $currentPage = $this->request->input('currentPage', 1);//当前页码
        $pageSize = $this->request->input('pageSize', 15);//每页记录条数

        $field = ["num"];
        if(!empty($device)){
            $field = [$device."_num as num"];
        }

        $where = [['type', '=', 'everyday']];
        $offset = ($currentPage - 1) * $pageSize;
        $columns = ['date','num','h5_num','applet_num','android_num','ios_num'];
        $tags = ['reg','new_office','publish_trad','notice_user'];

        $query = Db::table('statis_record')
            ->select($columns)
            ->where($where)
            ->whereIn('tag', $tags)
            ->groupBy('date');
        $total = $query->count();
        $datas = $query->offset($offset)
            ->limit($pageSize)
            ->orderBy('date', 'desc')
            ->get()
            ->toArray();
        $return_list = [];
        if (!empty($datas)) {
            foreach ($datas as $data) {
                $reg_data = Db::table('statis_record')
                    ->select($field)
                    ->where([['date', '=', $data['date']],['tag', '=', "reg"]])
                    ->first();
                $notice_user_data = Db::table('statis_record')
                    ->select($field)
                    ->where([['date', '=', $data['date']],['tag', '=', "notice_user"]])
                    ->first();

                $new_office_data = Db::table('office')
                    ->where([
                        ['created_at', '>=', strtotime($data['date'])],
                        ['created_at', '<=', strtotime($data['date']." 23:59:59")],
                        ['status', '=', 1],
                        ['is_del', '=', 0]
                    ])
                    ->count();
                $publish_trad_data = Db::table('office_calendar')
                    ->select(['id'])
                    ->where([
                        ['created_at', '>=', strtotime($data['date'])],
                        ['created_at', '<=', strtotime($data['date']." 23:59:59")],
                        ['status', '=', 1],
                        ['is_del', '=', 0]
                    ])
                    ->groupBy("office_id")
                    ->get()
                    ->toArray();

                $return_list[] = [
                    'date'=>$data['date'],
                    'reg_num'=>!empty($reg_data['num'])?$reg_data['num']:0,
                    'notice_user'=>!empty($notice_user_data['num'])?$notice_user_data['num']:0,
                    'new_office_num'=>!empty($new_office_data)?$new_office_data:0,
                    'publish_trad_num'=>!empty($publish_trad_data)?count($publish_trad_data):0
                ];
            }
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', ['total'=>$total, 'list'=>$return_list]);
    }
}